package com.dongwu.manager.service.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.dongwu.common.dao.DynamicSpecifications;
import com.dongwu.common.dao.SearchFilter;
import com.dongwu.common.service.impl.BaseServiceImpl;
import com.dongwu.common.utils.DateUtils;
import com.dongwu.manager.dao.DeviceProductRecordDao;
import com.dongwu.manager.entity.DeviceProductRecord;
import com.dongwu.manager.service.DeviceProductRecordService;

@Service
public class DeviceProductRecordServiceImpl extends BaseServiceImpl<DeviceProductRecord,String> implements DeviceProductRecordService {
	@Autowired
	private DeviceProductRecordDao deviceProductRecordDao;
	
	Class<DeviceProductRecord> entityClass = null;

	@SuppressWarnings("unchecked")
	public DeviceProductRecordServiceImpl() {
		Type t = getClass().getGenericSuperclass();
		if (t instanceof ParameterizedType) {
			Type[] p = ((ParameterizedType) t).getActualTypeArguments();
			entityClass = (Class<DeviceProductRecord>) p[0];
		}
	}

	@Override
	public List<DeviceProductRecord> getAllRecord(Map<String, Object> searchParams) {
		// TODO Auto-generated method stub
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<DeviceProductRecord> spec = DynamicSpecifications.bySearchFilter(filters.values(), entityClass);
		// 返回当前用户的所有投放记录
		List<DeviceProductRecord> devList = deviceProductRecordDao.findAll(spec);
		return devList;
	}

	@Override
	public Long getCount(Map<String, Object> searchParams, String userId) {
		Long devList=null;
		if(userId == null){
			// TODO Auto-generated method stub
			Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
			Specification<DeviceProductRecord> spec = DynamicSpecifications.bySearchFilter(filters.values(), entityClass);
			// 返回当前用户的所有投放记数
			devList = deviceProductRecordDao.count(spec);
		}else{
			devList = deviceProductRecordDao.countByUserid(userId);
		}
		return devList;
	}

	@Override
	public List<DeviceProductRecord> findByDeviceId(String deviceId) {
		List<DeviceProductRecord> records = deviceProductRecordDao.findByDeviceid(deviceId);
		// 循环判断当前时间是否在投放设定的时间内,如果不在当前时间内则删除
		if (records != null) {
			for (int i = 0; i < records.size(); i++) {
				// 如果两个时间都是null时则永久展示
				if (records.get(i).getStartTime() != null || records.get(i).getEndTime() != null) {
					// 开始和结束时间都不为空时
					if (records.get(i).getStartTime() != null && records.get(i).getEndTime() != null) {
						// 判断如果当前时间不在范围内删除这条
						if (!DateUtils.belongCalendarBetterwin(records.get(i).getStartTime(),
								records.get(i).getEndTime())) {
							records.remove(records.get(i));
							i = i - 1;
							continue;
						}
					}
					// 只设置了开始时间
					if (records.get(i).getStartTime() != null && records.get(i).getEndTime() == null) {
						// 判断如果当前时间在开始时间之前则不显示
						if (DateUtils.belongCalendarBefore(records.get(i).getStartTime())) {
							records.remove(records.get(i));
							i = i - 1;
							continue;
						}
					}
					// 只设置了结束时间（今天到结束之间为展示时间）
					if (records.get(i).getStartTime() == null && records.get(i).getEndTime() != null) {
						// 判断如果当前时间在结束时间之后则不显示
						if (DateUtils.belongCalendarAfter(records.get(i).getEndTime())) {
							records.remove(records.get(i));
							i = i - 1;
							continue;
						}
					}
				}
			}
		}
		return records;
	}
	
}
